﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Common.Util
{
    public class LimitQueue<T>
    {
        public LimitQueue(int size)
        {
            Size = size;
            Queue = new LinkedList<T>();
        }

        private LinkedList<T> Queue { get; }

        private int Size { get; set; }
        
        public LimitQueue<T> Add(T data)
        {
            if (Queue.Count == Size)
            {
                Queue.RemoveFirst();;
            }
            Queue.AddLast(data);
            return this;
        }
        
        public LimitQueue<T> AddRange(List<T> data)
        {
            foreach (var x1 in data)
            {
                if (Queue.Count == Size)
                {
                    Queue.RemoveFirst();
                }
                Queue.AddLast(x1);
            }
            return this;
        }

        public T GetFirstData()
        {
            return Queue.First.Value;
        }

        public T GetLastData()
        {
            return Queue.Last.Value;
        }
        public List<T> GetData()
        {

            return Queue.Select(m => m).ToList();
        }

        public int Count()
        {
            return Queue.Count;
        }

        public int LimitCount()
        {
            return Size;
        }
    }
}